#include"UdpServer.hpp"
#include<memory>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;

void Usage(string proc)
{
    cout<<"\n\rUsage: "<<proc<<" port[1024+]"<<endl;
}
string Handler(const string &str)
{
    string res="Server get a message: ";
    res+=str;
    cout<<res<<endl;

    
    return res;
}
bool SafeCheck(const string &cmd)
{
    int safe=false;
    vector<string> key_word={
        "rm",
        "mv",
        "cp",
        "kill",
        "sudo",
        "unlink",
        "unistall",
        "yum",
        "top",
        "whild"
    };
    for(auto &word:key_word)
    {
        auto pos=cmd.find(word);
        if(pos!=string::npos) return false;
    }
    return true;
}
string ExcuteCommand(const string& cmd)
{
    if(!SafeCheck(cmd)) return "Bad man";
    FILE* fp=popen(cmd.c_str(),"r");
    if(nullptr==fp)
    {
        perror("popen");
        return "error";
    }
    string result;
    char buffer[4096];
    while(true)
    {
        char *ok=fgets(buffer,sizeof(buffer),fp);
        if(ok==nullptr) break;
        result+=buffer;
    }
    pclose(fp);

    return result;
}
int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port=stoi(argv[1]);

    unique_ptr<UdpServer> svr(new UdpServer(port));

    svr->Init();
    svr->Run(Handler);
    return 0;
}